{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "55155365-701c-41e6-9079-51fb02bb2036",
   "metadata": {},
   "source": [
    "# Simulating Data From Bayesian Networks\n",
    "\n",
    "pgmpy implements the `DiscreteBayesianNetwork.simulate` method to allow users to simulate data from a fully defined Bayesian Network under various conditions. These conditions can be any combination of:\n",
    "1. Virtual Evidence\n",
    "2. Hard Evidence\n",
    "3. Virtual Intervention\n",
    "4. Hard Intervention\n",
    "\n",
    "Users can also provide data corresponding to some of the variables in the model (partial samples) to the simulation method. This allows users to fix the values of those variables to the specified value.\n",
    "\n",
    "Lastly, the user can also generate data with missing values, according to a user-defined CPD, to simulate realistic real-world data and evaluate how missingness affects inference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2feeb277",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A helper function to compute probability distributions from simulated samples.\n",
    "def get_distribution(samples, variables=None):\n",
    "    \"\"\"\n",
    "    For marginal distribution, P(A): get_distribution(samples, variables=['A'])\n",
    "    For joint distribution, P(A, B): get_distribution(samples, variables=['A', 'B'])\n",
    "    \"\"\"\n",
    "    if variables is None:\n",
    "        raise ValueError(\"variables must be specified\")\n",
    "\n",
    "    return samples.groupby(variables, observed=False).size() / samples.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cdb2e2f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Do not print warnings\n",
    "import logging\n",
    "from pgmpy.global_vars import logger\n",
    "logger.setLevel(logging.ERROR)\n",
    "\n",
    "# Specify the model to simulate data from.\n",
    "from pgmpy.factors.discrete import TabularCPD\n",
    "from pgmpy.utils import get_example_model\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "alarm = get_example_model(\"alarm\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c064112",
   "metadata": {},
   "source": [
    "## 1. Standard simulation\n",
    "\n",
    "Without any specified conditions for simulation, the `simulate` method draws samples from the joint distribution of the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "94e02da7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e8a7cb45d02c4ff8a26ee30e72907c77",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/37 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LVEDVOLUME</th>\n",
       "      <th>MINVOLSET</th>\n",
       "      <th>TPR</th>\n",
       "      <th>HYPOVOLEMIA</th>\n",
       "      <th>INTUBATION</th>\n",
       "      <th>HR</th>\n",
       "      <th>DISCONNECT</th>\n",
       "      <th>ARTCO2</th>\n",
       "      <th>ANAPHYLAXIS</th>\n",
       "      <th>LVFAILURE</th>\n",
       "      <th>...</th>\n",
       "      <th>HRBP</th>\n",
       "      <th>HRSAT</th>\n",
       "      <th>PVSAT</th>\n",
       "      <th>PAP</th>\n",
       "      <th>FIO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>ERRLOWOUTPUT</th>\n",
       "      <th>PULMEMBOLUS</th>\n",
       "      <th>HISTORY</th>\n",
       "      <th>HREKG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>...</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  LVEDVOLUME MINVOLSET     TPR HYPOVOLEMIA INTUBATION    HR DISCONNECT ARTCO2  \\\n",
       "0       HIGH    NORMAL  NORMAL        TRUE     NORMAL  HIGH       TRUE   HIGH   \n",
       "1        LOW    NORMAL  NORMAL       FALSE     NORMAL  HIGH       TRUE    LOW   \n",
       "2     NORMAL    NORMAL     LOW       FALSE     NORMAL  HIGH      FALSE   HIGH   \n",
       "3     NORMAL    NORMAL    HIGH       FALSE     NORMAL  HIGH      FALSE   HIGH   \n",
       "4     NORMAL    NORMAL     LOW       FALSE     NORMAL  HIGH      FALSE   HIGH   \n",
       "\n",
       "  ANAPHYLAXIS LVFAILURE  ...    HRBP HRSAT   PVSAT     PAP    FIO2    CO  \\\n",
       "0       FALSE     FALSE  ...    HIGH  HIGH  NORMAL  NORMAL  NORMAL  HIGH   \n",
       "1       FALSE      TRUE  ...  NORMAL  HIGH  NORMAL  NORMAL     LOW   LOW   \n",
       "2       FALSE     FALSE  ...    HIGH  HIGH     LOW  NORMAL  NORMAL  HIGH   \n",
       "3       FALSE     FALSE  ...    HIGH  HIGH     LOW  NORMAL  NORMAL  HIGH   \n",
       "4       FALSE     FALSE  ...    HIGH  HIGH     LOW  NORMAL  NORMAL  HIGH   \n",
       "\n",
       "  ERRLOWOUTPUT PULMEMBOLUS HISTORY HREKG  \n",
       "0        FALSE       FALSE   FALSE  HIGH  \n",
       "1         TRUE       FALSE   FALSE  HIGH  \n",
       "2        FALSE       FALSE   FALSE  HIGH  \n",
       "3        FALSE       FALSE   FALSE  HIGH  \n",
       "4        FALSE       FALSE   FALSE  HIGH  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "samples = alarm.simulate(n_samples=int(1e4))\n",
    "samples.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6d6664d",
   "metadata": {},
   "source": [
    "## 2. Simulation under specified evidence\n",
    "\n",
    "Specifying hard evidence for some variables fixes their values to the specified evidence value during simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6ebdeade",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "98a29e4de55e44298f3372e900f8b69f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LVEDVOLUME</th>\n",
       "      <th>MINVOLSET</th>\n",
       "      <th>TPR</th>\n",
       "      <th>HYPOVOLEMIA</th>\n",
       "      <th>INTUBATION</th>\n",
       "      <th>HR</th>\n",
       "      <th>DISCONNECT</th>\n",
       "      <th>ARTCO2</th>\n",
       "      <th>ANAPHYLAXIS</th>\n",
       "      <th>LVFAILURE</th>\n",
       "      <th>...</th>\n",
       "      <th>HRBP</th>\n",
       "      <th>HRSAT</th>\n",
       "      <th>PVSAT</th>\n",
       "      <th>PAP</th>\n",
       "      <th>FIO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>ERRLOWOUTPUT</th>\n",
       "      <th>PULMEMBOLUS</th>\n",
       "      <th>HISTORY</th>\n",
       "      <th>HREKG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>ONESIDED</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  LVEDVOLUME MINVOLSET     TPR HYPOVOLEMIA INTUBATION    HR DISCONNECT ARTCO2  \\\n",
       "0     NORMAL    NORMAL  NORMAL       FALSE     NORMAL  HIGH      FALSE    LOW   \n",
       "1     NORMAL    NORMAL  NORMAL       FALSE     NORMAL  HIGH      FALSE   HIGH   \n",
       "2     NORMAL    NORMAL    HIGH       FALSE     NORMAL  HIGH      FALSE   HIGH   \n",
       "3       HIGH    NORMAL     LOW       FALSE     NORMAL  HIGH      FALSE   HIGH   \n",
       "4       HIGH    NORMAL    HIGH        TRUE   ONESIDED  HIGH      FALSE   HIGH   \n",
       "\n",
       "  ANAPHYLAXIS LVFAILURE  ...    HRBP HRSAT PVSAT     PAP    FIO2    CO  \\\n",
       "0       FALSE     FALSE  ...    HIGH  HIGH  HIGH  NORMAL  NORMAL  HIGH   \n",
       "1       FALSE     FALSE  ...  NORMAL  HIGH   LOW  NORMAL  NORMAL   LOW   \n",
       "2       FALSE     FALSE  ...    HIGH  HIGH   LOW  NORMAL  NORMAL  HIGH   \n",
       "3       FALSE     FALSE  ...    HIGH  HIGH   LOW     LOW  NORMAL  HIGH   \n",
       "4       FALSE     FALSE  ...    HIGH  HIGH   LOW    HIGH  NORMAL   LOW   \n",
       "\n",
       "  ERRLOWOUTPUT PULMEMBOLUS HISTORY HREKG  \n",
       "0        FALSE       FALSE   FALSE  HIGH  \n",
       "1         TRUE       FALSE   FALSE  HIGH  \n",
       "2        FALSE       FALSE   FALSE  HIGH  \n",
       "3        FALSE       FALSE   FALSE  HIGH  \n",
       "4        FALSE       FALSE   FALSE  HIGH  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evidence = {\"CVP\": \"NORMAL\", \"HR\": \"HIGH\"}\n",
    "samples = alarm.simulate(n_samples=int(1e4), evidence=evidence)\n",
    "samples.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2587ee03-7466-4bd2-8f7b-9d7e32780637",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# All values of HR and CVP should be set to HIGH and NORMAL respectively.\n",
    "print(all(samples.HR == \"HIGH\"))\n",
    "print(all(samples.CVP == \"NORMAL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e58aaee7",
   "metadata": {},
   "source": [
    "## 3. Simulation under soft/virtual evidence\n",
    "\n",
    "Unlike hard evidence where the value of the specified variables is fixed to the specified evidence, virtual evidence allows users to set the marginal distribution of variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "edb1b6dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c64752b9df324c9bba706aca4f43e36f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# The virtual evidence is specified using TabularCPDs. Here, P(CVP=NORMAL) = 0.2, P(CVP=LOW) = 0.3, and P(CPV=HIGH) = 0.5\n",
    "cvp_evidence = TabularCPD(variable=\"CVP\",\n",
    "                          variable_card=3,\n",
    "                          values=[[0.2], [0.3], [0.5]],\n",
    "                          state_names={\"CVP\": [\"LOW\", \"NORMAL\", \"HIGH\"]})\n",
    "samples = alarm.simulate(n_samples=int(1e4), virtual_evidence=[cvp_evidence])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "30e2fb63-8ca2-472e-8c2e-3e32169ea9ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CVP\n",
       "HIGH      0.2445\n",
       "LOW       0.0689\n",
       "NORMAL    0.6866\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check the marginal distribution of CVP\n",
    "get_distribution(samples, variables=['CVP'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9332944a",
   "metadata": {},
   "source": [
    "## 4. Simulation under specified intervention\n",
    "\n",
    "Using the `do` argument, users can specify interventions to the model. The value of the intervened variables are set to the specified value and all incoming edges to these variables are removed in the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b89529e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "495480123fa045b486f059b690fc8d53",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LVEDVOLUME</th>\n",
       "      <th>MINVOLSET</th>\n",
       "      <th>TPR</th>\n",
       "      <th>HYPOVOLEMIA</th>\n",
       "      <th>INTUBATION</th>\n",
       "      <th>HR</th>\n",
       "      <th>DISCONNECT</th>\n",
       "      <th>ARTCO2</th>\n",
       "      <th>ANAPHYLAXIS</th>\n",
       "      <th>LVFAILURE</th>\n",
       "      <th>...</th>\n",
       "      <th>HRBP</th>\n",
       "      <th>HRSAT</th>\n",
       "      <th>PVSAT</th>\n",
       "      <th>PAP</th>\n",
       "      <th>FIO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>ERRLOWOUTPUT</th>\n",
       "      <th>PULMEMBOLUS</th>\n",
       "      <th>HISTORY</th>\n",
       "      <th>HREKG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 37 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  LVEDVOLUME MINVOLSET     TPR HYPOVOLEMIA INTUBATION    HR DISCONNECT ARTCO2  \\\n",
       "0     NORMAL    NORMAL    HIGH       FALSE     NORMAL  HIGH      FALSE    LOW   \n",
       "1       HIGH    NORMAL  NORMAL        TRUE     NORMAL  HIGH      FALSE   HIGH   \n",
       "2       HIGH    NORMAL     LOW        TRUE     NORMAL  HIGH      FALSE   HIGH   \n",
       "3     NORMAL    NORMAL     LOW       FALSE     NORMAL  HIGH       TRUE    LOW   \n",
       "4     NORMAL    NORMAL  NORMAL       FALSE     NORMAL  HIGH      FALSE   HIGH   \n",
       "\n",
       "  ANAPHYLAXIS LVFAILURE  ...  HRBP HRSAT PVSAT     PAP    FIO2    CO  \\\n",
       "0       FALSE     FALSE  ...  HIGH  HIGH  HIGH  NORMAL  NORMAL  HIGH   \n",
       "1       FALSE     FALSE  ...  HIGH  HIGH   LOW    HIGH  NORMAL   LOW   \n",
       "2       FALSE     FALSE  ...  HIGH  HIGH   LOW  NORMAL  NORMAL   LOW   \n",
       "3        TRUE     FALSE  ...  HIGH  HIGH  HIGH     LOW  NORMAL  HIGH   \n",
       "4       FALSE     FALSE  ...  HIGH  HIGH   LOW  NORMAL  NORMAL  HIGH   \n",
       "\n",
       "  ERRLOWOUTPUT PULMEMBOLUS HISTORY HREKG  \n",
       "0        FALSE       FALSE   FALSE  HIGH  \n",
       "1        FALSE       FALSE   FALSE  HIGH  \n",
       "2        FALSE       FALSE   FALSE  HIGH  \n",
       "3        FALSE       FALSE   FALSE  HIGH  \n",
       "4        FALSE       FALSE   FALSE  HIGH  \n",
       "\n",
       "[5 rows x 37 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "samples = alarm.simulate(n_samples=int(1e4), do={\"CVP\": \"NORMAL\", \"HR\": \"HIGH\"})\n",
    "samples.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b9fa901",
   "metadata": {},
   "source": [
    "## 5. Simulation under soft/virtual intervention\n",
    "\n",
    "Similar to virtual evidence, users can specify virtual intervention as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eab6d41b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a97536957fdb4c76be56a9d3659cf843",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/10000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CVP\n",
       "HIGH      0.3735\n",
       "LOW       0.2165\n",
       "NORMAL    0.4100\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cvp_intervention = TabularCPD(variable=\"CVP\",\n",
    "                              variable_card=3,\n",
    "                              values=[[0.2], [0.3], [0.5]],\n",
    "                              state_names={\"CVP\": [\"LOW\", \"NORMAL\", \"HIGH\"]})\n",
    "samples = alarm.simulate(n_samples=int(1e4), virtual_intervention=[cvp_intervention])\n",
    "get_distribution(samples, variables=[\"CVP\"])  # P(HISTORY)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acb80705-1b6d-46f0-81e5-d647231f28d7",
   "metadata": {},
   "source": [
    "## 6. Partial samples\n",
    "\n",
    "Users can also pass already generated data for some variables (for example, from some other simulation) to the simulation. This is equivalent to separately specifying evidence for each sample that is generate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e55792c2-7f57-4e1b-be8c-d0f5ba800dfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d1290a44e01c4ab1b56ad832ef216b32",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/37 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Generate some data on CVP.\n",
    "partial_cvp = pd.DataFrame(np.random.choice([\"LOW\", \"NORMAL\", \"HIGH\"], int(1e4)), columns=['CVP'])\n",
    "samples = alarm.simulate(n_samples=int(1e4), partial_samples=partial_cvp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bb58b071-0365-4bc3-a7d1-09ee955d19fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(all(samples[\"CVP\"] == partial_cvp[\"CVP\"]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e39b5a01",
   "metadata": {},
   "source": [
    "## 7. Simulate missing data\n",
    "\n",
    "Lastly, users can generate data with missing values for some specified variables, according to a user defined CPD. The name of the missing variable should be followed by a * to indicate missingness, and should contain 2 states: 1 (Missing) and 0 (Not Missing). Optionally, we can use the `return_full` argument to get back the removed values for comparison."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a01a31e",
   "metadata": {},
   "source": [
    "#### 7.1. Missing completely at random (MCAR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8ccae666",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9ec86e2131624c4484c5fab263c75f7c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/38 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LVEDVOLUME</th>\n",
       "      <th>MINVOLSET</th>\n",
       "      <th>TPR</th>\n",
       "      <th>CVP_full</th>\n",
       "      <th>HYPOVOLEMIA</th>\n",
       "      <th>INTUBATION</th>\n",
       "      <th>HR</th>\n",
       "      <th>DISCONNECT</th>\n",
       "      <th>ARTCO2</th>\n",
       "      <th>ANAPHYLAXIS</th>\n",
       "      <th>...</th>\n",
       "      <th>HRBP</th>\n",
       "      <th>HRSAT</th>\n",
       "      <th>PVSAT</th>\n",
       "      <th>PAP</th>\n",
       "      <th>FIO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>ERRLOWOUTPUT</th>\n",
       "      <th>PULMEMBOLUS</th>\n",
       "      <th>HISTORY</th>\n",
       "      <th>HREKG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  LVEDVOLUME MINVOLSET     TPR CVP_full HYPOVOLEMIA INTUBATION      HR  \\\n",
       "0     NORMAL      HIGH  NORMAL   NORMAL       FALSE     NORMAL  NORMAL   \n",
       "1       HIGH       LOW     LOW   NORMAL        TRUE     NORMAL    HIGH   \n",
       "2       HIGH    NORMAL  NORMAL   NORMAL        TRUE     NORMAL    HIGH   \n",
       "3     NORMAL    NORMAL  NORMAL   NORMAL       FALSE     NORMAL    HIGH   \n",
       "4     NORMAL    NORMAL  NORMAL   NORMAL       FALSE     NORMAL    HIGH   \n",
       "\n",
       "  DISCONNECT ARTCO2 ANAPHYLAXIS  ...    HRBP HRSAT PVSAT     PAP    FIO2  \\\n",
       "0      FALSE    LOW       FALSE  ...     LOW   LOW  HIGH  NORMAL  NORMAL   \n",
       "1      FALSE    LOW       FALSE  ...    HIGH  HIGH  HIGH  NORMAL  NORMAL   \n",
       "2      FALSE   HIGH       FALSE  ...    HIGH  HIGH   LOW  NORMAL     LOW   \n",
       "3      FALSE    LOW       FALSE  ...    HIGH  HIGH  HIGH     LOW  NORMAL   \n",
       "4      FALSE    LOW       FALSE  ...  NORMAL  HIGH   LOW  NORMAL  NORMAL   \n",
       "\n",
       "     CO ERRLOWOUTPUT PULMEMBOLUS HISTORY HREKG  \n",
       "0   LOW        FALSE       FALSE   FALSE   LOW  \n",
       "1   LOW        FALSE       FALSE   FALSE  HIGH  \n",
       "2   LOW        FALSE       FALSE   FALSE  HIGH  \n",
       "3  HIGH        FALSE       FALSE   FALSE  HIGH  \n",
       "4  HIGH         TRUE       FALSE   FALSE  HIGH  \n",
       "\n",
       "[5 rows x 38 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# CVP data missing completely randomly with 0.4 probability\n",
    "missing_CVP = TabularCPD(\n",
    "    variable=\"CVP*\",\n",
    "    variable_card=2,\n",
    "    values=[[0.6], \n",
    "            [0.4]], # Missing probability = 0.4\n",
    "    state_names={\"CVP*\": [0, 1]}\n",
    ")\n",
    "\n",
    "samples = alarm.simulate(n_samples=1000, missing_prob=[missing_CVP], return_full=True)\n",
    "samples.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c267953b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Missing values: 434/1000\n",
      "\n",
      "Original Distribution:\n",
      "CVP_full\n",
      "HIGH      0.158\n",
      "LOW       0.115\n",
      "NORMAL    0.727\n",
      "dtype: float64\n",
      "\n",
      "Distribution of Missing/Removed\n",
      "CVP_full\n",
      "HIGH      0.168203\n",
      "LOW       0.117512\n",
      "NORMAL    0.714286\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(f\"Missing values: {samples[\"CVP\"].isna().sum()}/{len(samples[\"CVP\"])}\")\n",
    "print()\n",
    "\n",
    "print(\"Original Distribution:\")\n",
    "print(get_distribution(samples, variables=\"CVP_full\"))\n",
    "print()\n",
    "print(\"Distribution of Missing/Removed\")\n",
    "print(get_distribution(samples.loc[samples[\"CVP\"].isna()], variables=\"CVP_full\")) # Since removal was completely random, we expect minimal change in distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf840260",
   "metadata": {},
   "source": [
    "#### 7.2. Missing at random (MAR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "89f1007d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f22980282b6d43a3b19b2b55ba58480c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/38 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LVEDVOLUME</th>\n",
       "      <th>MINVOLSET</th>\n",
       "      <th>TPR</th>\n",
       "      <th>CVP_full</th>\n",
       "      <th>HYPOVOLEMIA</th>\n",
       "      <th>INTUBATION</th>\n",
       "      <th>HR</th>\n",
       "      <th>DISCONNECT</th>\n",
       "      <th>ARTCO2</th>\n",
       "      <th>ANAPHYLAXIS</th>\n",
       "      <th>...</th>\n",
       "      <th>HRBP</th>\n",
       "      <th>HRSAT</th>\n",
       "      <th>PVSAT</th>\n",
       "      <th>PAP</th>\n",
       "      <th>FIO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>ERRLOWOUTPUT</th>\n",
       "      <th>PULMEMBOLUS</th>\n",
       "      <th>HISTORY</th>\n",
       "      <th>HREKG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>ONESIDED</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  LVEDVOLUME MINVOLSET     TPR CVP_full HYPOVOLEMIA INTUBATION      HR  \\\n",
       "0     NORMAL    NORMAL    HIGH   NORMAL       FALSE     NORMAL  NORMAL   \n",
       "1     NORMAL    NORMAL     LOW   NORMAL       FALSE     NORMAL    HIGH   \n",
       "2     NORMAL    NORMAL  NORMAL   NORMAL       FALSE     NORMAL    HIGH   \n",
       "3     NORMAL    NORMAL    HIGH   NORMAL       FALSE   ONESIDED    HIGH   \n",
       "4     NORMAL    NORMAL    HIGH   NORMAL       FALSE     NORMAL    HIGH   \n",
       "\n",
       "  DISCONNECT ARTCO2 ANAPHYLAXIS  ...    HRBP HRSAT PVSAT     PAP    FIO2  \\\n",
       "0      FALSE   HIGH       FALSE  ...     LOW   LOW   LOW  NORMAL     LOW   \n",
       "1      FALSE   HIGH       FALSE  ...    HIGH  HIGH   LOW  NORMAL  NORMAL   \n",
       "2      FALSE   HIGH       FALSE  ...  NORMAL  HIGH   LOW  NORMAL  NORMAL   \n",
       "3      FALSE   HIGH       FALSE  ...    HIGH  HIGH   LOW  NORMAL  NORMAL   \n",
       "4      FALSE   HIGH       FALSE  ...    HIGH  HIGH   LOW  NORMAL  NORMAL   \n",
       "\n",
       "       CO ERRLOWOUTPUT PULMEMBOLUS HISTORY HREKG  \n",
       "0  NORMAL        FALSE       FALSE   FALSE   LOW  \n",
       "1    HIGH        FALSE       FALSE   FALSE  HIGH  \n",
       "2     LOW         TRUE       FALSE   FALSE  HIGH  \n",
       "3    HIGH        FALSE       FALSE   FALSE  HIGH  \n",
       "4    HIGH        FALSE       FALSE   FALSE  HIGH  \n",
       "\n",
       "[5 rows x 38 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# CVP data missing depending on the observed LVEDVOLUME\n",
    "missing_CVP = TabularCPD(\n",
    "    variable=\"CVP*\",\n",
    "    variable_card=2,\n",
    "    values=[[0.8, 0.2, 0.7], \n",
    "            [0.2, 0.8, 0.3]], # Missing probabilities: LOW = 0.2, NORMAL = 0.8, HIGH = 0.3\n",
    "    evidence=[\"LVEDVOLUME\"],\n",
    "    evidence_card=[3],\n",
    "    state_names={\n",
    "        \"CVP*\": [0, 1],\n",
    "        \"LVEDVOLUME\": [\"LOW\", \"NORMAL\", \"HIGH\"]}\n",
    ")\n",
    "\n",
    "samples = alarm.simulate(n_samples=1000, missing_prob=[missing_CVP], return_full=True)\n",
    "samples.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1e66a7f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Missing values: 656/1000\n",
      "\n",
      "Original Distribution:\n",
      "LVEDVOLUME  CVP_full\n",
      "HIGH        HIGH        0.140\n",
      "            LOW         0.000\n",
      "            NORMAL      0.065\n",
      "LOW         HIGH        0.000\n",
      "            LOW         0.095\n",
      "            NORMAL      0.003\n",
      "NORMAL      HIGH        0.009\n",
      "            LOW         0.026\n",
      "            NORMAL      0.662\n",
      "dtype: float64\n",
      "\n",
      "Distribution of Missing/Removed\n",
      "LVEDVOLUME  CVP_full\n",
      "HIGH        HIGH        0.083841\n",
      "            LOW         0.000000\n",
      "            NORMAL      0.027439\n",
      "LOW         HIGH        0.000000\n",
      "            LOW         0.022866\n",
      "            NORMAL      0.001524\n",
      "NORMAL      HIGH        0.009146\n",
      "            LOW         0.032012\n",
      "            NORMAL      0.823171\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(f\"Missing values: {samples[\"CVP\"].isna().sum()}/{len(samples[\"CVP\"])}\")\n",
    "print()\n",
    "\n",
    "print(\"Original Distribution:\")\n",
    "print(get_distribution(samples, variables=[\"LVEDVOLUME\", \"CVP_full\"]))\n",
    "print()\n",
    "print(\"Distribution of Missing/Removed\")\n",
    "print(get_distribution(samples.loc[samples[\"CVP\"].isna()], variables=[\"LVEDVOLUME\", \"CVP_full\"])) # Since probability of missing is higher when LVEDVOLUME is \"NORMAL\" we expect distribution to be higher values there, and lesser otherwise"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d5d8e88",
   "metadata": {},
   "source": [
    "#### 7.3 Missing not at random (MNAR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d22d3a65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "373781b44c274eb7890cf82607e69dbb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/38 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LVEDVOLUME</th>\n",
       "      <th>MINVOLSET</th>\n",
       "      <th>TPR</th>\n",
       "      <th>CVP_full</th>\n",
       "      <th>HYPOVOLEMIA</th>\n",
       "      <th>INTUBATION</th>\n",
       "      <th>HR</th>\n",
       "      <th>DISCONNECT</th>\n",
       "      <th>ARTCO2</th>\n",
       "      <th>ANAPHYLAXIS</th>\n",
       "      <th>...</th>\n",
       "      <th>HRBP</th>\n",
       "      <th>HRSAT</th>\n",
       "      <th>PVSAT</th>\n",
       "      <th>PAP</th>\n",
       "      <th>FIO2</th>\n",
       "      <th>CO</th>\n",
       "      <th>ERRLOWOUTPUT</th>\n",
       "      <th>PULMEMBOLUS</th>\n",
       "      <th>HISTORY</th>\n",
       "      <th>HREKG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HIGH</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>TRUE</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>HIGH</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>...</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>LOW</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>NORMAL</td>\n",
       "      <td>LOW</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>FALSE</td>\n",
       "      <td>LOW</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  LVEDVOLUME MINVOLSET     TPR CVP_full HYPOVOLEMIA INTUBATION      HR  \\\n",
       "0     NORMAL    NORMAL  NORMAL   NORMAL       FALSE     NORMAL    HIGH   \n",
       "1     NORMAL    NORMAL     LOW   NORMAL       FALSE     NORMAL    HIGH   \n",
       "2     NORMAL    NORMAL    HIGH   NORMAL       FALSE     NORMAL    HIGH   \n",
       "3       HIGH    NORMAL  NORMAL   NORMAL        TRUE     NORMAL    HIGH   \n",
       "4       HIGH    NORMAL    HIGH     HIGH        TRUE     NORMAL  NORMAL   \n",
       "\n",
       "  DISCONNECT ARTCO2 ANAPHYLAXIS  ...  HRBP HRSAT PVSAT     PAP    FIO2  \\\n",
       "0      FALSE   HIGH       FALSE  ...  HIGH  HIGH   LOW  NORMAL  NORMAL   \n",
       "1       TRUE    LOW       FALSE  ...  HIGH  HIGH  HIGH  NORMAL  NORMAL   \n",
       "2      FALSE   HIGH       FALSE  ...  HIGH  HIGH   LOW  NORMAL  NORMAL   \n",
       "3       TRUE   HIGH       FALSE  ...  HIGH  HIGH   LOW  NORMAL  NORMAL   \n",
       "4      FALSE   HIGH       FALSE  ...   LOW   LOW   LOW  NORMAL  NORMAL   \n",
       "\n",
       "       CO ERRLOWOUTPUT PULMEMBOLUS HISTORY HREKG  \n",
       "0    HIGH        FALSE       FALSE   FALSE  HIGH  \n",
       "1    HIGH        FALSE       FALSE   FALSE  HIGH  \n",
       "2     LOW        FALSE       FALSE   FALSE  HIGH  \n",
       "3  NORMAL        FALSE       FALSE   FALSE  HIGH  \n",
       "4     LOW        FALSE       FALSE   FALSE   LOW  \n",
       "\n",
       "[5 rows x 38 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# CVP data missing depending on the unobserved original CVP value\n",
    "missing_CVP = TabularCPD(\n",
    "    variable=\"CVP*\",\n",
    "    variable_card=2,\n",
    "    values=[[0.2, 0.4, 0.6], \n",
    "            [0.8, 0.6, 0.4]], # Missing probabilities: LOW = 0.8, NORMAL = 0.6, HIGH = 0.4\n",
    "    evidence=[\"CVP\"],\n",
    "    evidence_card=[3],\n",
    "    state_names={\n",
    "        \"CVP*\": [0, 1],\n",
    "        \"CVP\": [\"LOW\", \"NORMAL\", \"HIGH\"]}\n",
    ")\n",
    "\n",
    "samples = alarm.simulate(n_samples=1000, missing_prob=[missing_CVP], return_full=True)\n",
    "samples.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4bfa7e50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Missing values: 605/1000\n",
      "\n",
      "Original Distribution:\n",
      "CVP_full\n",
      "HIGH      0.166\n",
      "LOW       0.130\n",
      "NORMAL    0.704\n",
      "dtype: float64\n",
      "\n",
      "Distribution of Missing/Removed\n",
      "CVP_full\n",
      "HIGH      0.112397\n",
      "LOW       0.173554\n",
      "NORMAL    0.714050\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(f\"Missing values: {samples[\"CVP\"].isna().sum()}/{len(samples[\"CVP\"])}\")\n",
    "print()\n",
    "\n",
    "print(\"Original Distribution:\")\n",
    "print(get_distribution(samples, variables=\"CVP_full\"))\n",
    "print()\n",
    "print(\"Distribution of Missing/Removed\")\n",
    "print(get_distribution(samples.loc[samples[\"CVP\"].isna()], variables=\"CVP_full\")) # Since probability of missing is higher when CVP is \"LOW\" and lower when \"CVP\" is high we expect missing distribution for \"LOW\" to be greater, and for \"HIGH\" to be lower"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
